<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2020/6/23
 * Time: 17:08
 */

use Workerman\Worker;
use Workerman\Connection\AsyncTcpConnection;
require_once __DIR__ . '/../Workerman/Autoloader.php';

$worker = new Worker();

/**
 * aes加密
 * @param $str
 * @return string
 */
function aesEncrypt($str,$ak,$sk)
{
    return base64_encode(openssl_encrypt($str,'AES-128-CBC',$ak, 1, $sk));
}

/**
 * aes解密
 * @param $str
 * @return string
 */
function aesDecrypt($str,$ak,$sk)
{
    return openssl_decrypt(base64_decode($str), 'AES-128-CBC',$ak, 1, $sk);
}

function createNonce($len){
    $strs="QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm";
    return substr(str_shuffle($strs),mt_rand(0,strlen($strs)-11),$len);
}
$ak = 'asdawsfesadfgews';
$sk = '3333111111111112';


$worker->onWorkerStart = function($worker){
    $local = '127.0.0.1:9501';
    $online = '129.211.114.112:55049';
    $con = new AsyncTcpConnection('ws://'.$local);
    $con->headers = array('access_key' =>'asdawsfesadfgews');
    $con->onConnect = function($con) {
        $ak = 'asdawsfesadfgews';
        $sk = '3333111111111112';
        #ping
      /*  $form = array(
            'ping'
        );
        $action = 'ping';
        #发送ping
        $message = array(
            'nonce'=>time(),
            'data'=>aesEncrypt(json_encode($form)),
            'action'=>$action,
            'class'=>'Server',
            'access_key'=>'VkhRW0C521ZMYuqF',
        );*/

           #变更账户
       /* $form = array(
            'user_id'=>17,
            'nickname'=>'张三',
            'head'=>'http://pic2.sc.chinaz.com/Files/pic/pic9/202006/bpic20542_s.jpg'
        );
        $action = 'updateUserInfo';

        $message = array(
            'nonce'=>time(),
            'data'=>aesEncrypt(json_encode($form)),
            'action'=>$action,
            'class'=>'Server',
            'access_key'=>'VkhRW0C521ZMYuqF',
        );*/
       #登录id29
     $extra = array(
            'access_key'=>$ak,
            'app_user_id'=>'jianjianxiansheng',
            'nickname'=>'jj',
            'head'=>'http://img.xmfish.com/attachment/upload/middle/67/2233367.jpg'
        );
        $action = 'login';

        $message = array(
            'nonce'=>time(),
            'content'=>' ',
             'action'=>$action,
            'extra'=>$extra
        );
        $con->send(aesEncrypt(json_encode($message),$ak,$sk));
        echo '发消息时间：'.date("Y-m-d H:i:s").PHP_EOL;
        $time_interval = 5;
        $i = 1;
        \Workerman\Timer::add($time_interval, function() use($ak,$sk,$con,$i)
        {
            #七牛授权
       /*     $extra = array(

            );
               $action = 'qiniuAuth';

            $message = array(
                'nonce'=>time(),
                'content'=>'DxOHay1593939803100.556885',
               'action'=>$action,
               'extra'=>$extra,
            );*/
            #单聊
    /*$extra = array(

            );
            $action = 'chat';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
                'to_app_user_id'=>'572402b0988e33369ad8ff0d3cd440a9',
                'type'=>1,
            );*/
       #一对一系统消息

           /* $extra = array(
                'asd'=>'fuck'
            );
            $action = 'systemMessage';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
                'to_app_user_id'=>'9a4297c33d9328934e496a4074863cfs',
                'type'=>1,
            );*/
        #群聊
          #{"type":1,"group_id":"19","content":"a","action":"groupChat","nonce":"1594191881721FQK5l9"}
      /*  $extra = array(

            );
            $action = 'groupChat';
            $message = array(
                'nonce'=>time(),
                'content'=>'自动化测试'.time(),
                'action'=>$action,
                 'extra'=>$extra,
                'group_id'=>'107',
                'type'=>1,
            );*/
         #创建群
   /*    $extra = array(
            'app_user_ids'=>'572f02b0988e3x369ad8ff0d3cd440a9,572f02b0988e33369ad8ff0d3cd440a9,583c0ab07a4d120d6d772958ca51afdc,9a4297c33d9328934e496a4074863cfs'
        );
            $action = 'createGroup';
            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
       #邀请入群
       /*  $extra = array(
                'app_user_ids'=>'9a4d97c33d9328934e496a4074863cf6,9a4297c33d9328934e496a4074863cfs',
               'group_id'=>'77'
            );
            $action = 'inviteJoinGroup';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
         #我的群
       /*$extra = array(
                'type'=>'0'
            );
            $action = 'myGroup';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*//*$extra = array(
                'type'=>'0'
            );
            $action = 'myGroup';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
        #群成员详情
          /*$extra = array(
                'group_id'=>'132',
            );
            $action = 'groupMemberList';

            $message = array(
                'nonce'=>time(),
                'content'=>'自动化测试'.time(),
                'action'=>$action,
                'extra'=>$extra,
                'group_id'=>'25',
                'type'=>1,
            );*/
            #获取客服
         /*  $action = 'getServiceGroup';
            $extra = array(

            );
            $message = array(
                'nonce'=>time(),
                'content'=>'自动化测试'.time(),
                'action'=>$action,
                'extra'=>$extra,
                'group_id'=>'25',
                'type'=>1,
            );*/

            #移除群成员
        /* $extra = array(
                'group_id'=>'45',
                'app_user_ids'=>'59a4297c33d9328934e496a4074863cfs'
            );
            $action = 'removeMember';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
           #退出群
         /* $extra = array(
                'group_id'=>'3',
            );
            $action = 'leaveGroup';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/

         #邀请入群
          /*  $extra = array(
                'group_id'=>'3',
                'app_user_ids'=>'jianjianxianshen1g'
            );
            $action = 'inviteJoinGroup';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
/*  $extra = array(
                'group_id'=>'3',
                'app_user_ids'=>'jianjianxianshen1g'
            );
            $action = 'inviteJoinGroup';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
        #获取群历史消息
   /*  $extra = array(
                'group_id'=>'107',
               // 'msg_id'=>1,
              'count'=>20
            );
            $action = 'groupChatHistory';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/

       #获取单聊历史消息
         /*   $extra = array(
                'app_user_id'=>'jianjianxiansheng',
                'msg_id'=>'dd099b1f641ab2ffca15ecdf315b5f3c',
                'count'=>20
            );
            $action = 'chatHistory';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
         #获取群详情
        /* $extra = array(
                'group_id'=>'1'
            );
            $action = 'getGroupDetail';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
         #获取客服群getServiceGroup
     /*    $extra = array(
            );
            $action = 'getServiceGroup';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
         #客服发消息
     /*  $extra = array(

            );
            $action = 'groupChat';

            $message = array(
                'nonce'=>time().'asfse',
                'content'=>'您好，客服售前咨询-高蚁材为您服务。😥🤫😪😪🥳💘😪🤔🤔🤔🤔'.time(),
                'action'=>$action,
                'extra'=>$extra,
                'group_id'=>'106',
                'type'=>1,
            );*/
        #加入黑名单
  /*          $extra = array(
                'app_user_id'=>'jianjianxianshen1g'
        );
            $action = 'getBlacklist';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
          /*  $extra = array(
                'group_id'=>'5',
                'msg_setting'=>2
            );
            $action = 'settingGroupMsg';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );*/
          #主动同步信息
          $condition = array(
               [ 'type'=>1,
                   'id'=>'572402b0988e33369ad8ff0d3cd440a9'
               ]
            );
            $extra = array(
                'condition'=>$condition,
            );
            $action = 'syncLastMsg';

            $message = array(
                'nonce'=>time(),
                'content'=>'你好啊',
                'action'=>$action,
                'extra'=>$extra,
            );

      $con->send(aesEncrypt(json_encode($message),$ak,$sk));

       #  echo '发送时间：'.date("Y-m-d H:i:s").PHP_EOL;
        });
    };

    $con->onMessage = function($con, $data) {
        $ak = 'asdawsfesadfgews';
        $sk = '3333111111111112';
        $dataArr = json_decode(aesDecrypt($data,$ak,$sk),true);

        echo date('Y-m-d H:i:s',$dataArr['time']/1000).PHP_EOL;
        echo '接收时间：'.date("Y-m-d H:i:s").PHP_EOL;
        var_dump(     $dataArr);
        file_put_contents('json',json_encode($dataArr).PHP_EOL,FILE_APPEND);
        echo PHP_EOL;
    };

    $con->connect();
};

Worker::runAll();